<!--
  功能：方向管理
  作者：licy
  邮箱：licy@briup.com
  时间：2020年11月30日 14:10:25
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='direction_list'>
    <!-- 按钮区域 -->
    <div class="btns">
      <el-row>
        <el-col :span="16">
          <el-form inline size="small">
            <el-form-item label="名称">
              <el-input v-model="param.name" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="loadDirections">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="8" style="text-align:right">
          <el-button type="primary" size="small" @click="toSaveHandler">新增</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 按钮区域结束 -->
    <!-- 表格 -->
    <div class="directions">
      <ul>
        <li class="direction" v-for="d in directionData.list" :key="d.id">
          <div class="photo">
            <img src="@/assets/mt/smart.jpg" alt="" v-if="!d.photo">
            <img v-else :src="d.photo" alt="">
          </div>
          <div class="right">
            <div class="title">{{d.name}}</div>
            <div class="brief">{{d.introduce}}</div>
            <div class="price">
              ￥ {{d.price}}
            </div>
            <div class="others">
              <div class="recommand">
                推荐指数 <span style="color:red"> {{d.level}}</span>
              </div>
              <div class="salary">
                第一年薪资水平  <span style="color:red">{{d.salary}}</span>
              </div>
            </div>
          </div>
          <div class="option">
            <div><el-button type="text" size="mini" @click="toDeleteHandler(d)">删除</el-button></div>
            <div><el-button type="text" size="mini" @click="toEditHandler(d)">修改</el-button></div>
          </div>
        </li>
      </ul>
    </div>
    <!-- 表格结束 -->
    <!-- 分页 -->
   <div class="page">
      <el-pagination background 
        layout="prev, pager, next,sizes" 
        small
        :total="directionData.total" 
        :page-size="directionData.pageSize" 
        :current-page="directionData.page"
        :page-sizes="[5, 10, 15, 20]"
        @size-change="handleSizeChange"
        @current-change="currentChangeHandler">
      </el-pagination>
   </div>
    <!-- 分页 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="70%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form :model="form" ref="directionForm" :rules="rules" label-width="80px" size="small">
          <div class="sub_title"><i class="el-icon-upload"></i> 基本信息</div>
          <el-row>
            <el-col :span="16">
              <el-form-item label="方向名称" prop="name">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item label="方向简介" prop="introduce">
                <el-input type="textarea" v-model="form.introduce"></el-input>
              </el-form-item>
              <el-form-item label="薪水范围" prop="salary">
                <el-select v-model="form.salary">
                  <el-option value="8k~12k" label="8k~12k"></el-option>
                  <el-option value="12k~18k" label="12k~18k"></el-option>
                  <el-option value="15k~25k" label="15k~25k"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="标准价格" prop="price">
                <el-select v-model="form.price">
                  <el-option value="12800" label="12800"></el-option>
                  <el-option value="16800" label="16800"></el-option>
                  <el-option value="18800" label="18800"></el-option>
                </el-select>
                
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="photoId">
                <div style="width:200px;height:120px;border-radius:5px;overflow:hidden;box-shadow:0 0 5px #ccc" >
                  <img :src="form.photo" alt="" width="100%">
                </div>
                <el-upload
                  class="upload-demo"
                  action="http://121.199.29.84:8001/file/upload"
                  :on-success="uploadSuccessHandler"
                  :limit="1"
                  :file-list="fileList">
                  <el-button type="text" size="mini">替换</el-button>
                  <div slot="tip" class="el-upload__tip">文件大小不允许超过3M</div>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="推荐指数" prop="level">
                <el-input v-model="form.level"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="学习周期" prop="period">
               <el-select v-model="form.period">
                  <el-option value="4个月" label="4个月"></el-option>
                  <el-option value="4.5个月" label="4.5个月"></el-option>
                  <el-option value="5个月" label="5个月"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="sub_title"><i class="el-icon-upload"></i> 详细介绍</div>
          <el-form-item label-width="0">
            <tinymce v-model="form.details" :height="340" v-if="visible" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitHandler">确定</el-button>
      </span>
    </Briupdrawer>
    <!-- 抽屉结束 -->
  </div>
</template>

<script>
import {get,post} from '@/utils/request'
import _ from 'lodash'
import Tinymce from '@/components/Tinymce'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
    visible:false,
    loading:false,
    title:"",
    param:{
      page:1,
      pageSize:5
    },
    directionData:{
      list:[]
    },
    fileList:[],
    form:{},
    rules:{
      name: [ { required: true, message: '请输入名称', trigger: 'blur' } ],
      introduce: [ { required: true, message: '请输入简介', trigger: 'blur' } ]
    }
   }
  },
  // 计算属性
  computed: {},
  components: { Tinymce },
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 上传成功
    uploadSuccessHandler(response){
      if(response.status == 200){
        this.$set(this.form,'photo','http://121.199.29.84:8888/group1/'+response.data.id);
      } else {
        this.$message({type:'error',message:'附件服务器异常！'})
        this.visible = false;
      }
    },
    // 确认提交处理函数
    submitHandler(){
      // 表单校验
      this.$refs['directionForm'].validate((valid) => {
        if (valid) {
          // 校验通过后，完成数据交互
          let url = '/direction/saveOrUpdate'
          post(url,this.form).then(response => {
            this.$message({type:"success",message:response.message})
            this.loadDirections();
            this.visible = false;
          })
        } else {
          return false;
        }
      });
    },
    // 去保存
    toSaveHandler(){
      this.title = "新增方向信息";
      this.form = {};
      this.fileList = [];
      this.visible = true;
    },
    // 去修改
    toEditHandler(row){
      this.fileList = [];
      // 修改抽屉标题
      this.title = "修改方向信息"
      // 将当前行数据的克隆对象赋值给当前form,这样可以避免修改表单数据导致表格数据发生变化。
      this.form = _.clone(row);
      // 显示抽屉
      this.visible = true;
    },
    // 去删除
    toDeleteHandler(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = '/direction/deleteById'
        get(url,{id:row.id}).then(response =>{
          this.$message({ type: 'success', message: '删除成功!' });
          this.loadDirections();
        })
      })
    },
    // 查询方向信息
    loadDirections(){
      // 添加loading,页面加载开始
      this.loading = true;
      let url = "/direction/pageQuery"
      get(url,this.param).then(response => {
        this.directionData = response.data;
        // 页面加载结束
        this.loading = false;
      })
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadDirections();
    },
    // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadDirections();
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['directionForm'].clearValidate();
    },
    // 处理visible变化的钩子函数，解决抽屉无法收缩的bug
    handlerVisibleChange(val) {
      this.visible = val
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // 加载方向信息
    this.loadDirections();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss">
.form_photo {
  border: 1px solid #ccc;
  height: 100px;
  border-radius: 5px;
  overflow: hidden;
  img {
    width: 100%;
  }
}
</style>
<style scoped lang="scss">
.direction_list {
  color: #666;
  font-size: 14px;

  
  .directions {
    .direction {
      display: flex;
      flex-direction: row;
      padding:1em 0;
      border-bottom: 1px solid #efefef;
      position: relative;
      .photo {
        flex-basis: 200px;
        height: 120px;
        overflow: hidden;
        img {
          width: 100%;
        }
      }
      .right {
        flex:1;
        padding:0 1em;
        .title {
          font-weight: bold;
          line-height: 2em;
        }
        .brief {
          font-size: 12px;
          line-height: 1.5em;
        }
        .others {
          display: flex;
          position: absolute;
          bottom: 2em;
          font-size: 12px;
          & > div {
            margin-right: 1em;
          }
        }
      }
      .option {
        flex-basis: 50px;
        padding-top: 40px;
      }
    }
  }
}
</style>
